package com.yida.gwj.common.utils;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.web.page.TableDataInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 转换
 * Created by lqq on 2020-09-22.
 */
public class GwjConvertUtil {

	public static <T, S> T convert(final S s, Class<T> clz) {
		return s == null ? null : BeanUtil.copyProperties(s, clz);
	}

	public static <T, S> List<T> convertList(List<S> s, Class<T> clz) {
		return s == null ? null : s.stream().map(vs -> BeanUtil.copyProperties(vs, clz)).collect(Collectors.toList());
	}

	public static <T, S> Set<T> convertSet(Set<S> s, Class<T> clz) {
		return s == null ? null : s.stream().map(vs -> BeanUtil.copyProperties(vs, clz)).collect(Collectors.toSet());
	}

	public static <T> TableDataInfo convertTable(TableDataInfo tableDataInfo, Class<T> clz) {
		tableDataInfo.setRows(GwjConvertUtil.convertList(tableDataInfo.getRows(), clz));
		return tableDataInfo;
	}

	/**
	 * 使用流遍历操作
 	 */
	public static <T> List<List<T>> partition(final List<T> list, final int size) {
		int limit = (list.size() + size - 1) / size;
		List<List<T>> mgList = new ArrayList<>();
		Stream.iterate(0, n -> n + 1).limit(limit).forEach(i -> {
			mgList.add(list.stream().skip(i * size).limit(size).collect(Collectors.toList()));
		});
		return mgList;
	}

}
